home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0005_Creating a temporary stack.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  1.3 KB  |  61 lines

  1. {
  2. The following code shows how to create a temporary stack
  3. for use during ISRs and in other situations where the
  4. default stack might not be big enough.
  5.  
  6. The following program intentionally creates a very small
  7. default stack by using the $M compiler directive. It then
  8. calls a function which pushes $2000 bytes onto the stack,
  9. thereby setting up a situation which should cause a 202 error.
  10.  
  11. To avoid the error, the program creates a temporary stack
  12. by allocating $4000 bytes on the heap and moving this value
  13. into SS. It then sets SP to point at the end of this new
  14. stack and proceeds to call the function, which does not
  15. fail because of the new temporary stack.
  16.  
  17. This code has been tested in both real and protected mode.
  18. }
  19.  
  20.  
  21. {$M $800, 0, $12000}
  22.  
  23. procedure BreakDefaultStack;
  24. var
  25.   a:array[0..$2000] of char;
  26. begin
  27.   Writeln('Hello');
  28. end;
  29.  
  30. var
  31.   aSS, aSP: Word;
  32.   Stck: pointer;
  33.  
  34. begin
  35.   getmem(Stck, $4000);
  36.   Word (Stck^) := 1;
  37.  
  38.   asm
  39.     mov aSP, sp
  40.     mov ax, ss
  41.     mov aSS, ax
  42.     mov ax, word ptr [Stck]
  43.     mov bx, word ptr [stck+2]
  44.     add ax, $4000-2
  45.     cli
  46.     mov ss, bx
  47.     mov sp, ax
  48.     sti
  49.   end;
  50.  
  51.   BreakDefaultStack;
  52.  
  53.   asm
  54.     mov ax, aSS
  55.     mov ss, ax
  56.     mov sp, aSP
  57.   end;
  58.  
  59.   freemem(Stck, $4000);
  60. end.
  61.